package com.example.park.weixin;

import com.example.park.config.SpringUtils;
import com.example.park.weixin.bean.TemplateData;
import com.example.park.weixin.bean.WxMssVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import static com.example.park.weixin.AccessToken.getAccessToken;

@Service
@Slf4j
public class WxPushServiceQcl {

    //用来请求微信的get和post
    @Autowired
    private RestTemplate restTemplate;

    public static Date date;

    private String getUrl() {
        //获取access_token
        String access_token = getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send" +
                "?access_token=" + access_token;
        return url;
    }

    public WxPushServiceQcl() {
        restTemplate = SpringUtils.getBean(RestTemplate.class);
    }

    public String pushPay(String openid, Double money, String payType, String remarks) {
        String url = getUrl();

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户openid
        wxMssVo.setTemplate_id("Y_7kM1o1H0kbhMRTe7nU0gPwN4iohajp92E1Be6L_BQ");//模版id

        Map<String, TemplateData> m = new HashMap<>(4);

        //keyword1：账单金额，keyword2：账单类型，keyword3：生成时间，keyword4：备注
        TemplateData keyword1 = new TemplateData();
        keyword1.setValue(money + "元");
        m.put("amount10", keyword1);
        wxMssVo.setData(m);

        TemplateData keyword2 = new TemplateData();
        keyword2.setValue(payType);
        m.put("thing18", keyword2);
        wxMssVo.setData(m);

        date = new Date();
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sDateFormat.format(date);
        TemplateData keyword3 = new TemplateData();
        keyword3.setValue(time);
        m.put("time20", keyword3);
        wxMssVo.setData(m);

        TemplateData keyword4 = new TemplateData();
        keyword4.setValue(remarks);
        m.put("thing9", keyword4);
        wxMssVo.setData(m);

        System.out.println(wxMssVo.getData());

        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();
    }

    public String pushAlert(String openid, String msg, String remarks) {
        String url = getUrl();

        //拼接推送的模版
        WxMssVo wxMssVo = new WxMssVo();
        wxMssVo.setTouser(openid);//用户openid
        wxMssVo.setTemplate_id("ozxjLIWnPg0Cg7XXn4Nu96A3P1Mi1_Hv3d1ju7iKy44");//模版id

        Map<String, TemplateData> m = new HashMap<>(3);

        //keyword1：报警时间，keyword2：报警内容，keyword3：设备地址
        date = new Date();
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sDateFormat.format(date);
        TemplateData keyword1 = new TemplateData();
        keyword1.setValue(time);
        m.put("time3", keyword1);
        wxMssVo.setData(m);

        TemplateData keyword2 = new TemplateData();
        keyword2.setValue(msg);
        m.put("thing4", keyword2);
        wxMssVo.setData(m);

        TemplateData keyword3 = new TemplateData();
        keyword3.setValue(remarks);
        m.put("thing5", keyword3);
        wxMssVo.setData(m);

        System.out.println(wxMssVo.getData());

        ResponseEntity<String> responseEntity =
                restTemplate.postForEntity(url, wxMssVo, String.class);
        log.error("小程序推送结果={}", responseEntity.getBody());
        return responseEntity.getBody();
    }

}
